<?php
include_once("Constants.php");
include_once("Entity/survey.php");

class survey_dao_base {

	function delete_survey($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("DELETE FROM survey WHERE survey_id = :survey_id");
			$stmt->bindParam(':survey_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function get_survey($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("SELECT * FROM  survey s WHERE s.survey_id = :survey_id");
			$stmt->bindParam(':survey_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$s = null;
			if($row = $stmt->fetch())
			{
				$s = new survey($row["survey_id"],$row["survey_name"],$row["survey_type_id"],$row["app_id"]);
			}
			
			$dbh->commit();
			$dbh = null;
			return $s;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_survey_list()
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aos = array();
			$stmt = $dbh->query("SELECT * FROM survey");
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$s = new survey($row["survey_id"],$row["survey_name"],$row["survey_type_id"],$row["app_id"]);
				$aos[] = $s;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aos;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function insert_survey($s)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("INSERT INTO survey (survey_name, survey_type_id,app_id)
							VALUE(:survey_name ,:survey_type_id,:app_id)");
							
							
			$stmt->bindParam(':survey_name', $s->survey_name);
			$stmt->bindParam(':survey_type_id', $s->survey_type_id);
			$stmt->bindParam(':app_id', $s->survey_app_id);
			
			
			$stmt->execute();
			$id = $dbh->lastInsertId();	
			$dbh->commit();
			$dbh = null;

			return $id;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function update_survey($s)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("UPDATE survey s 
						SET 
						s.survey_name = :survey_name,
						s.survey_type_id = :survey_type_id,
						s.app_id=:survey_app_id
						WHERE 
						s.survey_id = :survey_id");
			$stmt->bindParam(':survey_name', $s->survey_name);
			$stmt->bindParam(':survey_type_id', $s->survey_type_id);
			$stmt->bindParam(':survey_app_id', $s->survey_app_id);
			$stmt->bindParam(':survey_id', $s->survey_id);
			
			
			
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

}
?>